package Exercise;

/**
 * @author dx
 * @version 1.0
 * @date 2022/5/28 15:52
 * @description:  插入排序练习 insert1
 */
public class Insert1 {

    public static void main(String[] args) {
        int []test = {1,2,4,1,33,21,2,12,9};
        insert1(test);
        for(int x:test){
            System.out.println(x);
        }
    }

    public static void insert1(int []nums){

        int len = nums.length;
        for(int i = 1;i<len;i++){
            int temp = nums[i];
            int j = i-1;  //从第i-1个开始比较tmp
            for(;j>=0;j--) {
                if (temp < nums[j]) {
                    nums[j + 1] = nums[j];
                } else {
                    break;
                }
            }
            nums[j+1] = temp;
        }
    }
}
